package com.zzw.zim.security.user;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 登录用户信息
 *
 * @author XiaoJie
 * @date 2025/5/15 14:07
 */
@Data
public class UserDetail implements UserDetails {
	private static final long serialVersionUID = 1L;

	private String id;
	private String username;
	private String password;
	private String realName;
	private String email;
	private String mobile;
	private String orgId;
	private String tcName;
	private String tcPassword;
	private Integer status;
	private Integer superAdmin;

	/**
	 * 数据权限范围
	 * <p>
	 * null：表示全部数据权限
	 */
	private List<String> dataScopeList;
	/**
	 * 帐户是否过期
	 */
	private boolean isAccountNonExpired = true;
	/**
	 * 帐户是否被锁定
	 */
	private boolean isAccountNonLocked = true;
	/**
	 * 密码是否过期
	 */
	private boolean isCredentialsNonExpired = true;
	/**
	 * 帐户是否可用
	 */
	private boolean isEnabled = true;
	/**
	 * 拥有权限集合
	 */
	private Set<String> authoritySet;

	@Override
	@JsonIgnore
	public Collection<? extends GrantedAuthority> getAuthorities () {
		return authoritySet.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toSet());
	}

	@Override
	public boolean isAccountNonExpired () {
		return this.isAccountNonExpired;
	}

	@Override
	public boolean isAccountNonLocked () {
		return this.isAccountNonLocked;
	}

	@Override
	public boolean isCredentialsNonExpired () {
		return this.isCredentialsNonExpired;
	}

	@Override
	public boolean isEnabled () {
		return this.isEnabled;
	}
}
